//
//  TZYGoodsViewController.m
//  Moment
//
//  Created by dllo on 15/12/26.
//  Copyright © 2015年 片刻. All rights reserved.
//

#import "TZYGoodsViewController.h"

@interface TZYGoodsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, retain)NSMutableArray *goodsListArray;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, assign)NSInteger nextPage;

@end

@implementation TZYGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.headNameLabel.text = @"良品";
    
    self.goodsListArray = [NSMutableArray array];
    
    [self createTableView];
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
}



- (void)addHeaderRefresh
{
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderAction)];
    [header setImages:[Singleton shareDataBase].animationArray forState:MJRefreshStateRefreshing];
    
    
    header.lastUpdatedTimeLabel.hidden = YES;
    
    // 隐藏状态
    header.stateLabel.hidden = YES;
    
    
    self.myTableView.mj_header = header;
    
    [self.myTableView.mj_header beginRefreshing];
    
    
}

- (void)refreshHeaderAction
{
    self.nextPage = 0;// 记录页码的
    self.isUpLoad = NO;//标记为下拉操作
    // 重新请求数据
    [self getAFNetWorkingData];
}








//#pragma mark -- 设置下拉刷新
//-(void)addHeaderRefresh
//{
//    //当下拉时候会触发该方法
//    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        NSLog(@"下拉");
//        
//        self.nextPage = 0;// 记录页码的
//        self.isUpLoad = NO;//标记为下拉操作
//        // 重新请求数据
//        [self getAFNetWorkingData];
//        
//    }];
//    
//    // 进到应用自动刷新(可以放在viewdidload)
//    [self.myTableView.mj_header beginRefreshing];
//}



//#pragma mark -- 设置上拉刷新
//-(void)addFooterRefresh
//{
//    //当上拉时候触发该方法
//    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        NSLog(@"上拉");
//        
//        self.nextPage += 10;
//        self.isUpLoad = YES;
//        // 重新请求数据
//        [self getAFNetWorkingData];
//        
//    }];
//    
//}

#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    //    设置上拉刷新动画
    MJRefreshAutoGifFooter  *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    [footer setImages:[Singleton shareDataBase].animationArray forState:MJRefreshStateRefreshing];
    footer.refreshingTitleHidden = YES;
    footer.stateLabel.hidden = YES;
    
    self.myTableView.mj_footer = footer;
    [self.myTableView.mj_footer beginRefreshing];
}
-(void)refreshAction{
    NSLog(@"上拉");
    self.nextPage += 10;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getAFNetWorkingData];
}





- (void)getAFNetWorkingData
{
    NSString *urlStr = @"http://api2.pianke.me/pub/shop";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=%ld&version=3.0.6", self.nextPage];
    
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.goodsListArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            TZYGoodsList *goodsList = [[TZYGoodsList alloc] init];
            [goodsList setValuesForKeysWithDictionary:dic];
            [self.goodsListArray addObject:goodsList];
            
        }
        
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        [self.myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
}



- (void)createTableView
{
    self.myTableView = [[UITableView alloc] init];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    //self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    
    self.myTableView.rowHeight = GoodsCellHeight;
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[TZYGoodsCell class] forCellReuseIdentifier:@"TZYGoodsCell"];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.goodsListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"TZYGoodsCell";
    
    TZYGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    
    TZYGoodsList *goodsModel = [self.goodsListArray objectAtIndex:indexPath.row];
    
    cell.goodListModel = goodsModel;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TZYGoodDetailViewController *goodDetail = [[TZYGoodDetailViewController alloc] init];
    TZYGoodsList *goodsList = [self.goodsListArray objectAtIndex:indexPath.row];
    goodDetail.thingID = goodsList.contentid;
    [self.navigationController pushViewController:goodDetail animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
